package com.ruoyi.hospital.controller;

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.extra.mail.MailUtil;
import com.ruoyi.hospital.domain.YlArea;
import com.ruoyi.hospital.domain.YlDepartment;
import com.ruoyi.hospital.domain.YlDoctor;
import com.ruoyi.hospital.domain.dto.ScheduleDTO;
import com.ruoyi.hospital.service.IYlAreaService;
import com.ruoyi.hospital.service.IYlDepartmentService;
import com.ruoyi.hospital.service.IYlDoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.hospital.domain.YlSchedule;
import com.ruoyi.hospital.service.IYlScheduleService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 排班信息Controller
 *
 * @author ruoyi
 * @date 2022-02-23
 */
@RestController
@RequestMapping("/schedule")
public class YlScheduleController extends BaseController
{
    @Autowired
    private IYlScheduleService ylScheduleService;

    @Autowired
    private IYlDoctorService ylDoctorService;

    @Autowired
    private IYlAreaService ylAreaService;

    @Autowired
    private IYlDepartmentService ylDepartmentService;

    /**
     * 查询排班信息列表
     */
    @RequiresPermissions("hospital:schedule:list")
    @GetMapping("/list")
    public TableDataInfo list(YlSchedule ylSchedule)
    {
        startPage();
        List<YlSchedule> list = ylScheduleService.selectYlScheduleList(ylSchedule);
        return getDataTable(list);
    }

    /**
     * 查询排班信息列表
     */
    @GetMapping("/schedulelist")
    public TableDataInfo getList()
    {
        long startTime = System.currentTimeMillis(); //获取开始时间
        List<YlSchedule> list = ylScheduleService.selectAll();
        List<ScheduleDTO> scheduleDTOList = new ArrayList<>();
        for(int i = 0; i < list.size(); i++){
            ScheduleDTO scheduleDTO = new ScheduleDTO();
            YlDoctor ylDoctor = ylDoctorService.selectYlDoctorByDoctorId(Long.valueOf(list.get(i).getDoctorId()));
            YlArea ylArea = ylAreaService.selectYlAreaByAreaId(ylDoctor.getAreaId());
            YlDepartment ylDepartment = ylDepartmentService.selectYlDepartmentByDepartmentId(ylDoctor.getDepartmentId());
            scheduleDTO.setId(list.get(i).getSchedulingId());
            scheduleDTO.setTitle(ylDoctor.getName());
            scheduleDTO.setStart(list.get(i).getStart());
            scheduleDTO.setEnd(list.get(i).getEnd());
            scheduleDTO.setArea(ylArea.getName());
            scheduleDTO.setDepartment(ylDepartment.getName());
            scheduleDTO.setNum(list.get(i).getNum());
            if(ylDoctor.getState()==1) {
                scheduleDTO.setState("正常");
            } else {
                scheduleDTO.setState("停工");
            }
            scheduleDTOList.add(scheduleDTO);
        }
        long endTime = System.currentTimeMillis(); //获取结束时间
        System.out.println("程序运行时间：" + (endTime - startTime) + "ms"); //输出程序运行时间
        return getDataTable(scheduleDTOList);
    }

    /**
     * 导出排班信息列表
     */
    @RequiresPermissions("hospital:schedule:export")
    @Log(title = "排班信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YlSchedule ylSchedule)
    {
        List<YlSchedule> list = ylScheduleService.selectYlScheduleList(ylSchedule);
        ExcelUtil<YlSchedule> util = new ExcelUtil<YlSchedule>(YlSchedule.class);
        util.exportExcel(response, list, "排班信息数据");
    }

    /**
     * 获取排班信息详细信息
     */
    @RequiresPermissions("hospital:schedule:query")
    @GetMapping(value = "/{schedulingId}")
    public AjaxResult getInfo(@PathVariable("schedulingId") Long schedulingId)
    {
        return AjaxResult.success(ylScheduleService.selectYlScheduleBySchedulingId(schedulingId));
    }

    /**
     * 新增排班信息
     */
    @RequiresPermissions("hospital:schedule:add")
    @Log(title = "排班信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YlSchedule ylSchedule)
    {
        MailUtil.send("102805283@qq.com", "新的排班通知", "邮件来自Hutool测试", false);
        return toAjax(ylScheduleService.insertYlSchedule(ylSchedule));
    }

    /**
     * 修改排班信息
     */
    @RequiresPermissions("hospital:schedule:edit")
    @Log(title = "排班信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YlSchedule ylSchedule)
    {
        int i = ylScheduleService.updateYlSchedule(ylSchedule);
        YlSchedule ylSchedule2 = ylScheduleService.selectYlScheduleBySchedulingId(ylSchedule.getSchedulingId());
        if(ylSchedule2.getNum()<6 && ylSchedule2.getNum()>0) {
            MailUtil.send("102805283@qq.com", "挂号余数不足", "邮件来自Hutool测试", false);
        } else if (ylSchedule2.getNum()==0) {
            MailUtil.send("102805283@qq.com", "挂号余数为0", "邮件来自Hutool测试", false);
        } else {
            return null;
        }
        return toAjax(i);
    }

    /**
     * 删除排班信息
     */
    @RequiresPermissions("hospital:schedule:remove")
    @Log(title = "排班信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{schedulingIds}")
    public AjaxResult remove(@PathVariable Long[] schedulingIds)
    {
        return toAjax(ylScheduleService.deleteYlScheduleBySchedulingIds(schedulingIds));
    }
}

